<template>
  <div class="article-container">
    <!-- 导航栏 -->
    <van-nav-bar
      fixed
      left-arrow
      @click-left="$router.back()"
      title="文章详情"
    ></van-nav-bar>

    <!-- 加载中 loading -->
    <van-loading class="article-loading" v-if="!ArticleConent.art_id"/>

    <!-- 文章详情 -->
    <div class="detail">
      <h3 class="title">{{ArticleConent.title}}</h3>
      <div class="author">
        <van-image round width="1rem" height="1rem" fit="fill" :src="ArticleConent.aut_photo"/>
        <div class="text">
          <p class="name">{{ArticleConent.aut_name}}</p>
          <p class="time">{{$formatDate(ArticleConent.pubdate)}}</p>
        </div>
        <van-button
          round
          size="small"
          type="info"
          @click="attention"
        >{{ArticleConent.is_followed ?"＋关注":"取消关注"}}</van-button>
      </div>
      <div class="content">
        <div v-html="ArticleConent.content"></div>
      </div>
      <van-divider>END</van-divider>
      <div class="zan">
        <van-button round size="small"
         hairline type="primary"
          plain
          :icon="ArticleConent.attitude===1?'good-job':'good-job-o'"
          @click="pointLike">
          {{ArticleConent.attitude===1?"取消点赞":"点赞"}}</van-button>
        &nbsp;&nbsp;&nbsp;&nbsp;
        <van-button round size="small" hairline type="danger" plain icon="delete" @click="delLike">不喜欢</van-button>
      </div>
    </div>
        <Commet></Commet>
  </div>
</template>
<script>
import { getArticle, addLike, delLike, dislikes, undislikes, followUser, unFollowUser } from '@/api/articles'
import Commet from '@/views/Article/Commet'
export default {
  components: {
    Commet
  },
  data () {
    return {
      ArticleConent: {} // 显示的页面数据
    }
  },
  mounted () {
    console.log(this.$route.query.id, '获取的id')
    this.loadArticle()
  },
  methods: {
    // 页面渲染
    loadArticle () {
      // 调用接口
      getArticle(this.$route.query.id).then((res) => {
        console.log(res)
        this.ArticleConent = res.data.data // 回填真实数据
      }).catch((err) => {
        console.log(err)
      })
    },
    // 点赞
    pointLike () {
      if (this.ArticleConent.attitude === -1) {
        addLike(this.ArticleConent.art_id).then((res) => {
          console.log(res)
          this.$toast.success('点赞成功啦')
        }).catch((err) => {
          console.log(err)
        })
      } else {
        delLike(this.ArticleConent.art_id).then((res) => {
          console.log(res)
          this.$toast.fail('取消点赞成功')
        }).catch((err) => {
          console.log(err)
        })
      }
      this.ArticleConent.attitude = this.ArticleConent.attitude * -1
    },
    // 不喜欢
    delLike () {
      if (this.ArticleConent.attitude === -1) {
        dislikes(this.ArticleConent.art_id).then((res) => {
          console.log(res, '不喜欢')
          this.$toast.success('删除喜欢成功啦')
        }).catch((err) => {
          console.log(err)
        })
      } else {
        undislikes(this.ArticleConent.art_id).then((res) => {
          console.log(res)
          this.$toast.fail('取消点赞成功')
        }).catch((err) => {
          console.log(err)
        })
      }
    },
    // +关注
    attention () {
      if (this.ArticleConent.is_followed) {
        followUser(this.ArticleConent.aut_id).then((res) => {
          console.log(res)
          this.$toast.success('关注成功啦')
        }).catch((err) => {
          console.log(err)
        })
      } else {
        unFollowUser(this.ArticleConent.aut_id).then((res) => {
          console.log(res)
          this.$toast.fail('取消关注成功')
        }).catch((err) => {
          console.log(err)
        })
      }
      this.ArticleConent.is_followed = !this.ArticleConent.is_followed
    }
  }
}
</script>
<style lang='less' scoped>
.article-container{
  position: absolute;
  left: 0;
  top: 0;
  overflow-y: scroll;
  width: 100%;
  height: 100%;
}
.article-loading {
  padding-top: 100px;
  text-align: center;
}
.error{
  padding-top: 100px;
  text-align: center;
}
.detail {
  padding: 50px 10px;
  .title {
    font-size: 16px;
  }
  .zan{
    text-align: center;
  }
  .author {
    padding: 10px 0;
    display: flex;
    .text {
      flex: 1;
      padding-left: 10px;
      line-height: 1.3;
      .name {
        font-size: 14px;
        margin: 0;
      }
      .time {
        margin: 0;
        font-size: 12px;
        color: #999;
      }
    }
  }
  .content {
    font-size:14px;
    overflow: hidden;
    white-space: pre-wrap;
    word-break: break-all;
    /deep/ img{
      max-width:100%;
      background: #f9f9f9;
    }
  }
}
 /deep/.van-nav-bar .van-icon {
    color: white;
  }
</style>
